package com.ykt.common.config;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.kit.ResourceKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.ykt.controller.CaptchaController;
import com.ykt.controller.ClientVersionController;
import com.ykt.controller.GoodsController;
import com.ykt.controller.MerchantController;
import com.ykt.controller.OrderController;
import com.ykt.controller.RyController;
import com.ykt.controller.ServiceController;
import com.ykt.controller.StatisticsController;
import com.ykt.controller.UploadController;
import com.ykt.controller.WelcomeController;
import com.ykt.interceptor.AccessInterceptor;
import com.ykt.interceptor.ExceptionInterceptor;
import com.ykt.model.ClientVersion;
import com.ykt.model.Goods;
import com.ykt.model.GoodsCategory;
import com.ykt.model.Order;
import com.ykt.model.OrderItem;
import com.ykt.model.Shop;
import com.ykt.model.User;
import com.ykt.model.UserFeedback;

public class ApiConfig extends JFinalConfig {
    private final static Logger LOGGER = LoggerFactory.getLogger(ApiConfig.class);


    @Override
    public void configConstant(Constants me) {
        PropKit.use("config.properties"); // 加载配置文件

        me.setDevMode(true);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", WelcomeController.class);
        me.add("/merchant", MerchantController.class);
        me.add("/order", OrderController.class);
        me.add("/version", ClientVersionController.class);
        me.add("/goods", GoodsController.class);
        me.add("/ry", RyController.class);
        me.add("/upload", UploadController.class);
        me.add("/captcha", CaptchaController.class);
        me.add("/statistics", StatisticsController.class);
        
        me.add("/service", ServiceController.class);

        LOGGER.debug("初始化configRoute");
    }

    @Override
    public void configPlugin(Plugins me) {
        LOGGER.debug("初始化configPlugin");

        // DB连接池插件
        Map<String, String> conf = ResourceKit.readProperties("jdbc.properties");
        C3p0Plugin c3p0Plugin = new C3p0Plugin(conf.get("jdbcUrl"), conf.get("user"), conf.get("password").trim());
        me.add(c3p0Plugin);

        // DB操作插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        arp.addMapping("user", User.class);
        arp.addMapping("order", Order.class);
        arp.addMapping("order_item", OrderItem.class);
        arp.addMapping("goods", Goods.class);
        arp.addMapping("goods_category", GoodsCategory.class);
        arp.addMapping("shop", Shop.class);
        arp.addMapping("client_version", ClientVersion.class);
        arp.addMapping("user_feedback", UserFeedback.class);

        arp.setDevMode(true);
        arp.setShowSql(true);
        me.add(arp);

        // 缓存插件
        me.add(new EhCachePlugin());
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new AccessInterceptor());
        me.add(new ExceptionInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {
        // TODO Auto-generated method stub

    }

}
